package org.eclipseuseradmin.internal.ui.expressions;

import org.eclipse.core.expressions.PropertyTester;
import org.osgi.service.useradmin.Role;

public class RolePropertyTester extends PropertyTester {

	private final static String ATT_NAME = "name";

	private final static String ATT_PROPERTIES = "properties";

	public boolean test(Object receiver, String property, Object[] args,
			Object expectedValue) {

		if (!(receiver instanceof Role)) {
			return false;
		}

		Role role = (Role) receiver;
		if (property.equals(ATT_NAME)) {
			return toString(expectedValue).equalsIgnoreCase(role.getName());
		} else if (property.equals(ATT_PROPERTIES) && args.length == 1) {
			return toString(expectedValue).equalsIgnoreCase(
					(String) role.getProperties().get(args[0]));
		}
		return false;
	}

	private String toString(Object object) {

		if (object == null) {
			return "";
		}
		return object.toString();
	}

}
